﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VPLink2013.Framework
{
    public static class VPLink2013SPHelper
    {
        #region GetValue
        /// <summary>
        /// Format value of a field from a list item Sharepoint, for display
        /// </summary>
        /// <param name="item">List item Sharepoint</param>
        /// <param name="field">Field name</param>
        /// <returns>Value as string</returns>
        public static string GetMultiLineTextValue(SPListItem item, string field)
        {
            string value = string.Empty;
            try
            {
                SPFieldMultiLineText textField = item.Fields[field] as SPFieldMultiLineText;
                value = textField.GetFieldValueAsText(item[field]);
            }
            catch (Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetValue : " + field + " => " + exc.Message + "/" + exc.StackTrace);
            }
            return value;
        }
        public static string GetValue(SPListItem item, string field)
        {
            //ContextTrace.AddTrace("GetValue.List : " + item.ParentList.Title + " / ID : " + item.ID.ToString() + "; Champ : " + field);

            if (item == null)
            {
                return string.Empty;
            }
            string Valeur = string.Empty;
            try
            {
                if (item[field] == null)
                {
                    return string.Empty;
                }
                Valeur = ((string)item[field]).Trim();
            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetValue : " + field + " => " + exc.Message + "/" + exc.StackTrace);
            }
            if (Valeur == null)
            {
                Valeur = "";
            }
            //AddTrace("GetValue : " + field + " = " + Valeur);
            return Valeur;
        }
        /// <summary>
        /// Format value of a field from a list item Sharepoint, for display
        /// </summary>
        /// <param name="item">List item Sharepoint</param>
        /// <param name="field">Field name</param>
        /// <returns>Value as string</returns>
        public static string GetValueDate(SPListItem item, string field)
        {
            DateTime Valeur = new DateTime(1900, 1, 1);
            if (item == null)
            {
                return null;
            }
            if (item[field] == null)
            {
                return null;
            }

            try
            {
                Valeur = (DateTime)item[field];
            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetValueDate : " + field + " => " + exc.Message + "/" + exc.StackTrace);
            }
            //AddTrace("GetValueDate : " + field + " = " + Valeur.ToString("dd/MM/yyyy HH:mm:ss"));
            if (Valeur.Year > 1900)
            {
                return Valeur.ToString("dd/MM/yyyy");
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// Format value of a field from a list item Sharepoint, for display
        /// </summary>
        /// <param name="item">List item Sharepoint</param>
        /// <param name="field">Field name</param>
        /// <returns>Value as string</returns>
        public static string GetValueDouble(SPListItem item, string field)
        {
            double Valeur = 0;
            if (item == null)
            {
                return null;
            }
            if (item[field] == null)
            {
                return null;
            }


            try
            {
                Valeur = (double)item[field];
                return Valeur.ToString();
            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetValueDouble : " + field + " => " + exc.Message + "/" + exc.StackTrace);
            }
            //AddTrace("GetValueDouble : " + field + " = " + Valeur.ToString());
            return string.Empty;
        }
        /// <summary>
        /// Format value of a field from a list item Sharepoint, for display
        /// </summary>
        /// <param name="item">List item Sharepoint</param>
        /// <param name="field">Field name</param>
        /// <returns>Value as string</returns>
        public static string GetValueBoolean(SPListItem item, string field)
        {
            bool Valeur = false;
            if (item == null)
            {
                return null;
            }
            if (item[field] == null)
            {
                return null;
            }

            try
            {
                Valeur = (bool)item[field];
            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetValueBoolean : " + field + " => " + exc.Message + "/" + exc.StackTrace);
            }
            //AddTrace("GetValueBoolean : " + field + " = " + Valeur.ToString());
            if (Valeur)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        public static string GetValueLookupId(SPListItem item, string field)
        {
            if (item == null)
            {
                return null;
            }
            if (item[field] == null)
            {
                return null;
            }
            string Valeur = "";
            try
            {
                SPFieldLookupValue lookupValue = (SPFieldLookupValue)item[field];
                Valeur = lookupValue.LookupId.ToString();
            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetValueLookupId : " + field + " => " + exc.Message + "/" + exc.StackTrace);
            }
            return Valeur;
        }

        public static string GetValueLookupLabel(SPListItem item, string field)
        {
            if (item == null)
            {
                return null;
            }
            if (item[field] == null)
            {
                return null;
            }
            string Valeur = "";
            try
            {
                SPFieldLookupValue lookupValue = (SPFieldLookupValue)item[field];
                Valeur = lookupValue.LookupValue;
            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetValueLookupLabel : " + field + " => " + exc.Message + "/" + exc.StackTrace);
            }
            return Valeur;
        }

        public static string GetValueAfterSeparator(string Valeur)
        {
            if (string.IsNullOrEmpty(Valeur))
            {
                return string.Empty;
            }
            int iPos = Valeur.IndexOf(";#");
            if (iPos >= 0)
            {
                Valeur = Valeur.Substring(iPos + 2);
            }
            iPos = Valeur.IndexOf(", ");
            if (iPos >= 0)
            {
                Valeur = Valeur.Substring(iPos + 2);
            }
            return Valeur;
        }
        public static string GetValueBeforeSeparator(string Valeur)
        {
            if (string.IsNullOrEmpty(Valeur))
            {
                return string.Empty;
            }
            int iPos = Valeur.IndexOf(";#");
            if (iPos >= 0)
            {
                Valeur = Valeur.Substring(0, iPos);
            }
            iPos = Valeur.IndexOf(", ");
            if (iPos >= 0)
            {
                Valeur = Valeur.Substring(0, iPos);
            }
            return Valeur;
        }



        /// <summary>
        /// Format value of a field from a list item Sharepoint, for display
        /// </summary>
        /// <param name="item">List item Sharepoint</param>
        /// <param name="field">Field name</param>
        /// <returns>Value as string</returns>
        public static string GetValueURL(SPListItem item, string field)
        {
            if (item == null)
            {
                return null;
            }

            try
            {
                if (item[field] == null)
                {
                    return null;
                }
                string Value = GetValue(item, field);
                string ValueAfter = GetValueAfterSeparator(Value);
                string ValueBefore = GetValueBeforeSeparator(Value);
                Value = "<a href=\"" + ValueBefore + "\">" + ValueAfter + "</a>";
                //AddTrace("GetValueURL : " + field + " = " + Value);
                return Value;
            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetValueURL : " + field + " => " + exc.Message + "/" + exc.StackTrace);
            }

            return "";
        }

        public static string GetValuePhoto(SPListItem item, string fieldName)
        {
            string strPhoto = GetValue(item, fieldName);
            strPhoto = GetValueBeforeSeparator(strPhoto).Replace(",", "");
            ContextTrace.AddTrace("GetValuePhoto.strPhoto : " + strPhoto);
            if (strPhoto.Trim() == "")
            {
                strPhoto = null;
            }
            return strPhoto;
        }

        #endregion
    }
}
